<?php

namespace Application\model;

use Cola\Model;

class QuestionnaireModel extends Model
{
    /**
     * 通过问卷ID获取配置信息
     * @param $qnid
     * @return array
     * @throws \Cola\Exception\DbException
     */
    public function getConfig($qnid)
    {
        $data = [];
        // 获取问卷信息
        $sql = "select id as qnid,title,items from gm_questionnaire where id = '$qnid'";
        $questionnaire = $this->db->row($sql);
        if ($questionnaire) {
            $data['info'] = $questionnaire;
            $sql = "select qnid,qsid,title,content,index_id from cfg_questionnaire where qnid = '$qnid'";
            $questionnaires = $this->db->sql($sql);
            $qs = [];
            if ($questionnaires) {
                $i=0;
                $contents = [];
                foreach ($questionnaires as $q) {
                    $contents[$q['qsid']][] = $q;
                }
                foreach ($contents as $qid => $c) {
                    $cs = [];
                    foreach ($c as $q) {
                        $cs[] = $q['content'];
                    }
                    $qs[$i]['qnid'] = $q['qnid'];
                    $qs[$i]['qsid'] = $q['qsid'];
                    $qs[$i]['title'] = $q['title'];
                    $qs[$i]['content'] = implode('|', $cs);
                    $i++;
                }
            }
            $data['subject'] = $qs;
        }
        return $data;
    }

    /**
     * 插入log表数据
     * @return array
     * @throws \Cola\Exception\DbException
     */
    public function insertData($datas)
    {
        $res = 0;
        if ($datas) {
            $res = $this->db('_backdb')->minsert($datas,'log_questionnaire');
        }

        return $res;
    }
}